<?php
/* *
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
*/
class chat_controller extends lietou
{
	function index_action(){
	    
	    if ($this->config['sy_chat_open']==1){
	        
	        $this -> public_action();
	        
	        $chatM  =  $this -> MODEL('chat');
	        
	        if (isset($_GET['id']) && isset($_GET['type']) && $this->uid && $this->usertype){
	            
	            $uid    =  intval($_GET['id']);
	            
	            $chat   =  $chatM->userinfo(array('uid'=>$uid,'usertype'=>intval($_GET['type']),'nowid'=>$this->uid,'nowtype'=>$this->usertype));
	            
	            $this -> yunset('receive',$chat['mine']);
	            
	            $chatM -> upChatLog(array('status'=>1),array('from'=>$uid,'to'=>$this->uid,'fusertype'=>$chat['mine']['usertype'],'tusertype'=>$this->usertype));
	            
	            //条件聊天判断时否有和个人聊天的权利
	            if ($this->config['sy_chat_type']==2){
	                
	                $right  =  $chatM->getChatRight(array('uid'=>$uid,'comid'=>$this->uid,'usertype'=>$this->usertype));
	                
	                if (empty($right)){
	                    // 没有和个人聊天权利时，判断套餐数量。如有，直接聊天；没有，返回聊天列表。
	                    $canChat  =  true;
	                    
	                    $statisM  =  $this->MODEL('statis');
	                    
	                    $statis   =  $statisM->getInfo($this->uid, array('usertype'=>$this->usertype,'field'=>'`vip_etime`,`rating_type`,`chat_num`'));
	                    
	                    if($statis['vip_etime'] >= strtotime('today') || $statis['vip_etime'] == '0'){
	                        
	                        if($statis['rating_type'] == 1){  // 套餐模式
	                            
	                            if($statis['chat_num'] > 0){ // 弹出购买提示
	                                
	                                $statisM->upInfo(array('chat_num'=>array('-',1)),array('uid'=>$this->uid,'usertype'=>$this->usertype));
	                                
	                                $chatM->addChatRight(array('uid'=>$uid,'comid'=>$this->uid,'ctime'=>time(),'usertype'=>$this->usertype));
	                                
	                            }else{
	                                
	                                $canChat  =  false;
	                            }
	                            
	                        }else if($statis['rating_type'] == 2){    //时间会员
	                            
	                            $chatM->addChatRight(array('uid'=>$uid,'comid'=>$this->uid,'ctime'=>time(),'usertype'=>$this->usertype));
	                        }
	                    }else{
	                        
	                        $canChat  =  false;
	                    }
	                    
	                    if (!$canChat){
	                        
	                        $this->ACT_msg('index.php?c=chat', '您没有与TA'.$this->config['sy_chat_name'].'的权限，请先购买'.$this->config['sy_chat_name']);
	                    }
	                }
	            }
	        }
	        
	        $myself   =  $chatM->userinfo(array('uid'=>$this->uid,'usertype'=>$this->usertype,'history'=>1));
	        
	        $this -> yunset('rows',$myself['history']);
	        
	        $this->yuntpl(array('chat/yunliao/index'));
	    }else{
	        
	        $this->ACT_msg('网站未开启'.$this->config['sy_chat_name'].'功能');
	    }
	}
	/**
	 * 判断聊天套餐情况
	 */
	function chatRight_action()
	{
	    
	    $_POST['uid']       =   $this->uid;
	    $_POST['spid']      =   $this->spid;
	    $_POST['username']  =   $this->username;
	    $_POST['usertype']  =   $this->usertype;
	    $comtcM             =   $this->MODEL('comtc');
	    
	    $return             =   $comtcM->chatRight($_POST);
	    
	    if ($return['status']) {
	        echo json_encode($return);
	    }
	}
}
?>